home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / grafix / 3d / scalekey.lha / ScaleKey / SK_Amiga.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-19  |  3.9 KB  |  206 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5.  
  6. FILE *in,*out;
  7. char infile[20],outfile[20];
  8. char temp[20]="sk.$$$";
  9. int SourceFrames,DestFrames;
  10. char buffer[1000];
  11. char t[40],t1[40];
  12.  
  13. void parse_args(int argc, char *argv[]);
  14. void print_usage(void);
  15. int getline(char *s, FILE *f);
  16. void Getword(char *s, char *t);
  17. void writeword(char *s);
  18. void writenumber(int n);
  19. static char version[]="$VER: Scalekey 1.0 Amiga ";
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23.  int sf,df,sft,dft,l,f;
  24.  int len=0;
  25.  float ratio;
  26.  
  27.  parse_args(argc, argv);
  28.  if ((in=fopen(infile,"rt"))==NULL) {
  29.   printf("Cannot open input file %s\n",strupr(infile));
  30.   exit(1);
  31.  }
  32.  if ((out=fopen(temp,"wt"))==NULL) {
  33.   printf("Error opening temp file.\n");
  34.   exit(1);
  35.  }
  36.  getline(buffer,in);
  37.  Getword(buffer,t);
  38.  if (strcmp(t,"STAGE")!=0) {
  39.   printf("Input file is not Imagine Staging Language file.\n");
  40.   fclose(out);
  41.   fclose(in);
  42.   remove(temp);
  43.   exit(1);
  44.  }
  45.  writeword(t);
  46.  fputc('\n',out);
  47.  getline(buffer,in);
  48.  Getword(buffer,t);
  49.  writeword(t);
  50.  Getword(buffer,t);
  51.  SourceFrames=atoi(t);
  52.  writenumber(DestFrames);
  53.  fputc('\n',out);
  54.  ratio=(float)DestFrames/(float)SourceFrames;
  55.  f=(ratio<1) ? 1 : 0;
  56.  while (!feof(in))
  57.  {
  58.  getline(buffer,in);
  59.  while(buffer[0]!='\n') {
  60.   Getword(buffer,t);
  61.   if (strcmp(t,"FRAMES")==0) {
  62.    writeword(t);
  63.    Getword(buffer,t);
  64.    Getword(buffer,t1);
  65.    sf=atoi(t);
  66.    df=atoi(t1);
  67.    l=df-sf;
  68.    if (sf!=1&&df!=1)
  69.    {
  70.     sft=len+1;
  71.     dft=ceil(df*ratio);
  72.     if (dft>DestFrames) dft=DestFrames;
  73.     if (dft==DestFrames)
  74.      len=0;
  75.     else
  76.      len=dft;
  77.    }
  78.    else if(sf==1)
  79.    {
  80.     sft=sf;
  81.     dft=ceil(df*ratio);
  82.     if (dft>DestFrames) dft=DestFrames;
  83.     if (dft==DestFrames)
  84.      len=0;
  85.     else
  86.      len=dft;
  87.    }
  88.    else if(sf==1&&df==1)
  89.    {
  90.     sft=1;
  91.     dft=1;
  92.     if (dft==DestFrames)
  93.      len=0;
  94.     else
  95.      len=1;
  96.    }
  97.    writenumber(sft);
  98.    writenumber(dft);
  99.   }
  100.   else
  101.    writeword(t);
  102.  }
  103.  fputc('\n',out);
  104.  }
  105.  fclose(in);
  106.  fclose(out);
  107.  if (strcmp(infile,outfile)==0)
  108.  {
  109.   remove(infile);
  110.   rename(temp,outfile);
  111.  }
  112.  else
  113.  {
  114.   remove(outfile);
  115.   rename(temp,outfile);
  116.   remove(temp);
  117.  }
  118.  free(t);
  119. }
  120.  
  121. void parse_args(int argc, char *argv[])
  122. {
  123.  printf("\nScaleKey utility for Imagine using Imaging Staging Language.\n");
  124.  printf("Version 1.0 Copyright (C) 1995 Andrey Zmievskiy. Jan 1995\n\n");
  125.  printf("Version 1.0 Amiga compiled Joop van de Wege Jan 1995\n\n");
  126.  if (argc<3) {
  127.   print_usage();
  128.  }
  129.  strcpy(infile,"");
  130.  strcpy(outfile,"");
  131.  strcpy(infile,argv[1]);
  132.  if (argv[2][0]=='-') {
  133.   DestFrames=atoi(&argv[2][1]);
  134.   strcpy(outfile,infile);
  135.   return;
  136.  }
  137.  strcpy(outfile,argv[2]);
  138.  if (argv[3][0]=='-') {
  139.   DestFrames=atoi(&argv[3][1]);
  140.   if (DestFrames<2) {
  141.    printf("You cannot use numbers less than 2 for frames.\n");
  142.    exit(1);
  143.   }
  144.  }
  145.  else
  146.   print_usage();
  147. }
  148.  
  149. void print_usage(void)
  150. {
  151.    printf("Usage:     scalekey <filename> [outputfile] -<number of frames>\n");
  152.    printf("Input filename should be staging file in ASCII format.\n");
  153.    printf("Ex: scalekey stage stage.1 -180\n");
  154.    exit(1);
  155. }
  156.  
  157. int getline(char *s, FILE *f)
  158. {
  159.  fgets(s,256,f);
  160. }
  161.  
  162. void Getword(char *s, char *tmp)
  163. {
  164.   int i=0;
  165.   int k;
  166.   int j=0;
  167.  
  168.   while (s[i]==' ') {
  169.    for(k=0;s[k]!='\n'; k++)
  170.     s[k]=s[k+1];
  171.   }
  172.   while(s[i]!=' '&&s[i]!='\n')
  173.   {
  174.    tmp[j++]=s[i];
  175.    for(k=i;s[k]!='\n';k++)
  176.     s[k]=s[k+1];
  177.   }
  178.   tmp[j]='\0';
  179. }
  180.  
  181. void writeword(char *s)
  182. {
  183.  fputs(s,out);
  184.  fputc(' ',out);
  185. }
  186.  
  187. void writenumber(int n)
  188. {
  189.  char s[20];
  190.  
  191.  itoa(n,s,10);
  192.  fputs(s,out);
  193.  fputc(' ',out);
  194. }
  195.  
  196. /* Dummy function because neither Aztec nor SAS/C do have this function  */
  197. /* Seems to work well. I know I have seen this function 'itoa' somewhere */
  198. /* but can't find it anywhere.                                           */
  199. /* Could make the executable much smaller if made more Amiga specific    */
  200. /* LIKE: use 'Write' or 'puts' instead of 'printf'                       */
  201.  
  202. itoa(int n, char *s, int number)
  203. {
  204.     sprintf(s, "%d",n);
  205. }
  206.